package com.jc.service.appinterface;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.ExceptionUtil;
import com.jc.common.util.HttpUtil;
import com.jc.common.util.JSONUtil;
import com.jc.common.util.MD5;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.model.AppRespModel;
import com.jc.service.apilog.ApiLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@SuppressWarnings("all")
@Service
public class AppInterfaceServiceImpl implements AppInterfaceService {

    private final Logger LOGGER = LoggerFactory.getLogger(AppInterfaceServiceImpl.class);
    private Long ts = new Date().getTime();
    @Value("${jicheng.app.url}")
    private String baseurl;
    @Value("${jicheng.app.code}")
    private String code;
    @Value("${jicheng.app.key}")
    private String key;
    @Autowired
    private ApiLogService apiLogService;


    @Override
    public AppRespModel AppInterfaceHttp(String url,Map<String,Object> body,LogType logType) {
        AppRespModel retMode = new AppRespModel();
        boolean isOK = false;
        String ret = null;
        try {
            url = baseurl + url;
            //拼接请求参数字符串
            body.put("code", code);
            body.put("encryptedString", MD5.passmd5("code="+code+"&ts="+String.valueOf(ts)+"&key="+key));
            body.put("ts",ts);
            ret = HttpUtil.post(url, body);
            
            
            LOGGER.error("请求VRT社区  ======== url : {} ,  param: {}, result: {} ", url, JSONObject.toJSONString(body) , ret);
            
            
            if(StringUtils.isBlank(ret)) {
            	retMode.setReturnCode("1");
                retMode.setReturnMsg("调用APP失败, VRT社区返回值为空!");
                return retMode;
            }
            
            if(!JSONUtil.isJson(ret)) {
            	retMode.setReturnCode("1");
                retMode.setReturnMsg("调用APP失败, VRT社区返回值格式!");
                return retMode;
            }
            
            JSONObject retObject = JSONObject.parseObject(ret);
            if(retObject != null) {
                retMode.setReturnCode(retObject.getString("returnCode"));
                retMode.setReturnMsg(retObject.getString("returnMsg"));
                retMode.setData(retObject);
                if(StringUtils.equals("0", retObject.getString("returnCode"))) {
                    isOK = true;
                }
            }else {
                isOK = false;
                retMode.setReturnCode("1");
                retMode.setReturnMsg("调用APP失败, 返回值为空!");
            }

            if(isOK) {
                apiLogService.addApiLog(logType,JSONObject.toJSONString(body),Conclusion.OK,"",ret);
            }else {
                apiLogService.addApiLog(logType,JSONObject.toJSONString(body),Conclusion.FAIL,"",ret);
            }
            return retMode;
        }catch (Exception e) {
            LOGGER.error("调用APP过程中出现网络错误, {},{}" , e.getMessage(),e);
            retMode.setReturnCode("1");
            if(e != null && StringUtils.isNotBlank(e.getMessage())) {
                retMode.setReturnMsg("调用失败 : " + StringUtils.substring(e.getMessage(), 0,150));
            }
            apiLogService.addApiLog(logType,JSONObject.toJSONString(body), Conclusion.FAIL,ExceptionUtil.subExceptionMessage(e, 0, 200) ,ret);
            return retMode;
        }
    }
}
